package com.kws.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kws.common.core.enums.ModelOpType;
import com.kws.common.core.sys.Ksys;
import com.kws.common.core.utils.KConstants;
import com.kws.common.core.utils.StringUtils;
import com.kws.common.security.utils.SecurityUtils;
import com.kws.system.domain.SysDepartureAddress;
import com.kws.system.domain.info.QueryDepartureAddressInfo;
import com.kws.system.domain.vo.CreateDepartureAddressRq;
import com.kws.system.domain.vo.UpdateDepartureAddressRq;
import com.kws.system.mapper.SysDepartureAddressMapper;
import com.kws.system.service.SysDepartureAddressService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <b>SysDepartureAddressServiceImpl</b>
 *
 * @description: SysDepartureAddressServiceImpl <br>
 * @date: 2024/11/6 18:36 <br>
 * @author: lhm <br>
 * @version: 1.0 <br>
 */

@Service
public class SysDepartureAddressServiceImpl extends ServiceImpl<SysDepartureAddressMapper, SysDepartureAddress> implements SysDepartureAddressService {
    private static final Logger logger = LoggerFactory.getLogger(SysDepartureAddressServiceImpl.class);

    @Override
    public Page<QueryDepartureAddressInfo> queryDepartureAddressPage(Integer pageNum, Integer pageSize, String consignerName) {
        Page<QueryDepartureAddressInfo> page = new Page<>(pageNum, pageSize);
        Page<QueryDepartureAddressInfo> pageInfo = baseMapper.queryDepartureAddressPage(page,consignerName);
        return pageInfo;
    }

    @Override
    public boolean createDepartureAddress(CreateDepartureAddressRq rq) {
        SysDepartureAddress sysDepartureAddress = new SysDepartureAddress();
        sysDepartureAddress.setUserId(rq.getUserId());
        sysDepartureAddress.setConsignerName(rq.getConsignerName());
        sysDepartureAddress.setDepartureAddress(rq.getDepartureAddress());
        sysDepartureAddress.setContactNumber(rq.getContactNumber());
        sysDepartureAddress.setMerchantId(rq.getMerchantId());
        if (StringUtils.isNotEmpty(rq.getChannel()) && "sys".equals(rq.getChannel())){
            sysDepartureAddress.setEnable("1");
            sysDepartureAddress.setCreateUser("sys");
            sysDepartureAddress.setCreateTime(new Date());
            sysDepartureAddress.setUpdateUser("sys");
            sysDepartureAddress.setUpdateTime(new Date());
        }else {
            Ksys.setBasicModelData(sysDepartureAddress, SecurityUtils.getStringUserId(), KConstants.DATA_ENABLE, ModelOpType.CREATE);
        }
        int insert = baseMapper.insert(sysDepartureAddress);
        if (1 == insert){
            return true;
        }
        return false;
    }

    @Override
    public QueryDepartureAddressInfo queryDepartureAddressById(Long id) {
        return baseMapper.queryDepartureAddressById(id);
    }

    @Override
    public boolean updateDepartureAddress(UpdateDepartureAddressRq rq) {
        SysDepartureAddress sysDepartureAddress = baseMapper.selectById(rq.getId());
        sysDepartureAddress.setUserId(rq.getUserId());
        sysDepartureAddress.setConsignerName(rq.getConsignerName());
        sysDepartureAddress.setDepartureAddress(rq.getDepartureAddress());
        sysDepartureAddress.setContactNumber(rq.getContactNumber());
        sysDepartureAddress.setMerchantId(rq.getMerchantId());
        Ksys.setBasicModelData(sysDepartureAddress,SecurityUtils.getStringUserId(),KConstants.DATA_ENABLE,ModelOpType.UPDATE);
        int i = baseMapper.updateById(sysDepartureAddress);
        if (1 == i){
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteDepartureAddressById(Long id) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id",id);
        updateWrapper.set("enable","0");
        updateWrapper.set("update_user",SecurityUtils.getStringUserId());
        updateWrapper.set("update_time",new Date());
        int update = baseMapper.update(null, updateWrapper);
        if (1 == update){
            return true;
        }
        return false;
    }
}
